package web.servlet;

import domain.Account;
import domain.Cart;
import domain.Order;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class EditOrderInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        //获得购物车
        Cart cart = (Cart) session.getAttribute("cart");
        //用户已登录，则产生新订单
        Order order = new Order();
        order.initOrder(loginAccount,cart);

        //接收js文件中Ajax传来的编辑的地址信息的data，更新该订单的地址信息
        String cardType = req.getParameter("cardType");
        String creditCard = req.getParameter("creditCard");
        String expiryDate = req.getParameter("expiryDate");
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String address1 = req.getParameter("address1");
        String address2 = req.getParameter("address2");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zip = req.getParameter("zip");
        String country = req.getParameter("country");

        order.setCardType(cardType);
        order.setCreditCard(creditCard);
        order.setExpiryDate(expiryDate);
        order.setShipToFirstName(firstName);
        order.setShipToLastName(lastName);
        order.setShipAddress1(address1);
        order.setShipAddress2(address2);
        order.setShipCity(city);
        order.setShipState(state);
        order.setShipZip(zip);
        order.setShipCountry(country);

        session.setAttribute("order",order);

        out.print("Success");

    }
}
